package com.example.administrator.myapplication;


import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button text;
    private Button text1;
    private Button text2;
    private Button text3;
    private Button text4;
    private Button text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (Button) findViewById(R.id.text);
        text1 = (Button) findViewById(R.id.text1);
        text2 = (Button) findViewById(R.id.text2);
        text3 = (Button) findViewById(R.id.text3);
        text4 = (Button) findViewById(R.id.text4);
        text5 = (Button) findViewById(R.id.text5);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop_up();
            }
        });
        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMultiDia();
            }
        });

        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                The_list_of();
            }
        });
        text3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               progress();
            }
        });
        text4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeww();
            }
        });
        text5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                multi_select();
            }
        });
    }


    public void pop_up() {
        new AlertDialog.Builder(MainActivity.this).setTitle("点击我")//设置对话框标题
                .setMessage("下面我们来点击“取消”或者“确定”吧！")//设置显示的内容
                .setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this,"很好 很听话！！",Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("返回",new DialogInterface.OnClickListener() {//添加返回按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {//响应事件
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this,"很好 很听话！！",Toast.LENGTH_SHORT).show();
            }
        }).show();//在按键响应事件中显示此对话框
    }

    private void showMultiDia()
    {
        final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择省份：");
        /*！注意这里的方法以及参数哦！*/
        builder.setItems(provices, new DialogInterface.OnClickListener() {
            public String provice;
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                provice = provices[arg1];
                Toast.makeText(MainActivity.this, "选择的城市为：" + provices[arg1],
                        Toast.LENGTH_SHORT).show();
                        /*提醒测试作用*/
            }
        });
        builder.create().show();
    }

    public void The_list_of(){
        final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("单选按钮对话框");
        /*！注意这里的方法以及参数哟！"0"表示弹出对话框时，第一个是被选中的*/
        builder.setSingleChoiceItems(provices, 0,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this,
                                "选择的城市是：" + provices[arg1], Toast.LENGTH_SHORT)
                                .show();
                                /*提醒测试作用*/
                    }
                });
        builder.create().show();

    }

    public void multi_select(){

        final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
        final boolean[] bs={false,false,false,false,false};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("复选按钮对话框");
        /*！注意这里的方法以及参数0，boolean数组的个数要与第一个参数的数组个数要对应！*/
        builder.setMultiChoiceItems(provices, bs,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1,
                                        boolean arg2) {
                        /*注意字符串数组provices和布尔数组bs的一一对应关系*/
                    }
                });
        builder.create().show();
    }

    public void timeww(){
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
            /*arg1表示年份，arg2表示几月（比实际月份要少一），arg3表示几号*/

                Toast.makeText(MainActivity.this,
                        "设置的时间为：" + arg1 + "年" + (arg2 + 1) + "月" + arg3 + "日",
                        Toast.LENGTH_SHORT).show();
                        /*提示检测作用*/
            }
        };
        /*注意这里声明对话框的方式与其他的对话框的区别*/
        DatePickerDialog datePickerDialog=new DatePickerDialog(this, dateSetListener, 2015, 10, 30);
        datePickerDialog.show();
        /*这里的第三个参数为年份，第四个为几月，在对话框显示是11月，自动加一，第五个是几号*/
    }


    public void progress(){

/*注意这里声明对话框的方式与其他的对话框的区别*/
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("进度条对话框");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        /*这里表示添加最大进度，后面可以通过setprogress方法修改进度*/
        progressDialog.setMax(100);
        /*这里的button可以随便set，不限制个数*/
        progressDialog.setButton("Hide", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface Dialog, int which) {
                //这里添加点击这个按钮后的事件
            }
        });
        progressDialog.setButton2("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        //这里添加点击这个按钮后的事件
                    }
                });

        progressDialog.show();


    }
}

